/**
 * 全局工具类
 */
import $http from "@/http";
import {API} from "@/aconst/API";
import {
    getApiVersion,
    getHtmlVersion,
    removeAllCache,
    setApiVersion,
    setHtmlVersion,
    setOpenErrorLog,
    setOpenWarnLog
} from "@/utils/storage";
import {warningMsg} from "@/utils/message";
import router from "@/router/router";
import {getTree} from "@/utils/math";
import BaseVue from "@/utils/BaseVue";
import {TagsViewModule} from "@/store/modules/tags-view";

// * 全局工具类
class Kit extends BaseVue {
    to(url: any = "/", query: any = {}) {
        // if (router.currentRoute.fullPath.indexOf(url) != -1) {
        //     router.push({path: url, query: query} as any);
        //     window.location.reload();
        //     return;
        // }
        router.push({path: url, query: query} as any);
    };

    back(url: any = "/", query: any = {}) {
        if (router.app && (router.app as any)._route) {
            TagsViewModule.delViewByRoute((router.app as any)._route);
        }
        router.push({path: url, query: query} as any);
    };

    getRoleName(id: any): any {
        if (!id) return "";
        if (id == "11") return "政府部门";
        if (id == "12") return "企事业单位";
        if (id == "13") return "个人用户";
        if (id == "21") return "应用供应商";
    }

    /**
     * NULL.调用 炸裂
     */
    getUrlByIndex(arrayObj: any, field: any = "url", index: any = 0): any {
        if (!arrayObj) {
            return "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png";
        }
        if (typeof arrayObj == "string") {
            arrayObj = JSON.parse(arrayObj);
        }
        if (arrayObj[index]) {
            return arrayObj[index][field];
        } else {
            return "https://cube.elemecdn.com/9/c2/123432132.png";
        }
    }

    getUrlArray(arrayAny: any, type: any = null): any {
        let array = [];
        if (!arrayAny) {
            array.push("https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png");
            return array;
        }

        //
        let objArray;
        if (typeof arrayAny == "string") {
            objArray = JSON.parse(arrayAny);
        } else {
            objArray = arrayAny;
        }

        //
        if (type == "FULL") {
            for (let obj of objArray) {
                array.push(obj.fullUrl);
            }
            return array;
        }

        if (!type) {
            for (let obj of objArray) {
                array.push(obj.url);
            }
            return array;
        }
    };


    nullDefault(str: any, str2: any): any {
        if (str) {
            return str
        }
        return str2;
    }

    nullDefault2(str: any, str2: any, str3: any): any {
        if (str) {
            return str
        }
        if (str2) {
            return str2
        }
        return str3;
    }

    parsJson(str: any): any {
        if (!str) return {};

        if (typeof str == "string") {
            return JSON.parse(str);
        }

        return str;
    }

    parsJsonArray(str: any): any {
        if (!str) return [];

        if (typeof str == "string") {
            let parse = JSON.parse(str);
            if (parse.length == 0) {
                return [];
            }
            return parse;
        }

        if (typeof str == "object") return str;

        return []
    }

    strToArray(str: any, split: any = null): any {
        let array: any = [];
        if (!str) {
            return array;
        }

        array.push(str);
        return array;
    }

    splitToStr(str: any, split: any = null): any {
        if (!str) {
            return [];
        }
        let array = [];
        let strings = str.toString().split(split);

        for (let a of strings) {
            // console.log(a);
            array.push(a.toString());
        }
        return array;
    }

    strToArray2(str: any, str2: any, split: any = null): any {
        if (str) {
            let array: any = [];
            array.push(str);
            return array;
        }

        if (str2) {
            let array: any = [];
            array.push(str2);
            return array;
        }

        return [];
    }


    /**
     * 删除字符 开头或结尾 是这个字符
     * @param originalStr 原始字符串
     * @param delStr 匹配字符串
     */
    delFirstAndLastFirst(originalStr: any, delStr: any): string {
        return this.delLastStr(this.delFirstStr(originalStr, delStr), delStr);
    };

    /**
     * 删除字符 开头 是这个字符
     * @param originalStr 原始字符串
     * @param delStr 匹配字符串
     */
    delFirstStr(originalStr: any, delStr: any): string {
        if (originalStr.charAt(0) == delStr) {
            return originalStr.substring(1, originalStr.length)
        }
        return originalStr;
    };

    /**
     * 删除字符 结尾 是这个字符
     * @param originalStr 原始字符串
     * @param delStr 匹配字符串
     */
    delLastStr(originalStr: any, delStr: any): string {
        if (originalStr.charAt(originalStr.length - 1) == delStr) {
            return originalStr.substring(0, originalStr.length - 1)
        }
        return originalStr;
    };

    getMd5(obj: any): string {
        return "";
    }

    tree(obj: any): Array<any> {
        return JSON.parse(getTree(obj));
    }

    douHaoToArray(str: any): Array<any> {
        if (str && str.toString().length > 0) {
            return str.toString().split(",");
        }
        return [] as any;
    }

    arrayToDouHao(array: Array<any>): string {
        if (array.length == 0) {
            return "";
        }
        let str = "";
        for (let c of array) {
            str += (c + ",");
        }
        return str.substring(0, str.length - 1);
    }

}

// $K 全局工具类
const $K = new Kit();
export default $K


const message = "服务已更新，稍后将重新登录,请您谅解.";
/**
 * 刷新字典
 */
export const refreshDict = (then: any = () => {
}) => {
    $http.post(API.refreshDict, {})
        .then((resDict: any) => {
            if (resDict.success) {
                let oldHtmlVersion = getHtmlVersion();
                let oldApiVersion = getApiVersion();

                setHtmlVersion(resDict.data.adminHtmlVersion);
                setApiVersion(resDict.data.adminApiVersion);
                setOpenErrorLog(resDict.data.openErrorLog);
                setOpenWarnLog(resDict.data.openWarnLog);

                //检查 版本号 服务端版本号
                if (oldApiVersion != resDict.data.adminApiVersion && oldHtmlVersion != resDict.data.adminHtmlVersion) {
                    if (oldHtmlVersion != "0" || oldApiVersion != "0") {
                        warningMsg(message, 3333);
                        removeAllCache();
                        setTimeout(() => {
                            window.location.reload();
                        }, 3333);
                    }
                    return "";
                }
                //检查版本号
                if (oldHtmlVersion != resDict.data.adminHtmlVersion) {
                    if (oldHtmlVersion != "0") {
                        window.location.reload();
                    }
                    return "";
                }

                //检查服务端版本号
                if (oldApiVersion != resDict.data.adminApiVersion) {
                    if (oldApiVersion != "0") {
                        warningMsg(message, 3333);
                        removeAllCache();
                        setTimeout(() => {
                            window.location.reload();
                        }, 3333);
                        return "";
                    }
                    return "";
                }


            }
            return "";
        })
};

/**
 * 内容是否为空
 * @param fileData 文件数组
 * @param minCount 最小数量，默认不判断
 * @param maxCount 最大数量，默认不判断
 */
export const fileDataIsNull = (fileData: any, minCount: any = null, maxCount: any = null) => {
    if (!fileData || fileData.length == 0) return true;
    if (minCount) {
        if (fileData.length < minCount * 1) return true;
    }
    if (maxCount) {
        if (fileData.length > minCount * 1) return true;
    }
    return false;
};

export const quickPickerOptions = {
    shortcuts: [{
        text: '最近一周',
        onClick(picker: any) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
        }
    }, {
        text: '最近一个月',
        onClick(picker: any) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
        }
    }, {
        text: '最近三个月',
        onClick(picker: any) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
        }
    }]
};

export const todayYmd = () => {
    let dateTime = new Date();
    const YY = dateTime.getFullYear();
    const MM =
        dateTime.getMonth() + 1 < 10
            ? '0' + (dateTime.getMonth() + 1)
            : dateTime.getMonth() + 1;
    const D =
        dateTime.getDate() < 10 ? '0' + dateTime.getDate() : dateTime.getDate();
    return `${YY}-${MM}-${D}`;
};

export const parsDay1 = (str: any) => {
    if (str == "MONDAY") return "1";
    if (str == "TUESDAY") return "2";
    if (str == "WEDNESDAY") return "3";
    if (str == "THURSDAY") return "4";
    if (str == "FRIDAY") return "5";
    if (str == "SATURDAY") return "6";
    if (str == "SUNDAY") return "7";
};
export const parsDay2 = (str: any) => {
    if (str == "MONDAY") return "一";
    if (str == "TUESDAY") return "二";
    if (str == "WEDNESDAY") return "三";
    if (str == "THURSDAY") return "四";
    if (str == "FRIDAY") return "五";
    if (str == "SATURDAY") return "六";
    if (str == "SUNDAY") return "日";
};
export const parsDay3 = (str: any) => {
    if (str == "1") return "一";
    if (str == "2") return "二";
    if (str == "3") return "三";
    if (str == "4") return "四";
    if (str == "5") return "五";
    if (str == "6") return "六";
    if (str == "7") return "日";
};


export const sortMapKeyAsc = (map: any) => {
    if (!map) return new Map();

    let list: any = [];
    for (let key1 of map.keys()) {
        list.push(key1);
    }
    let sortAsc1 = sortAsc(list);


    let newMap = new Map();
    for (let key2 of sortAsc1) {
        newMap.set(key2, map.get(key2));
    }

    return newMap;
};

export const sortMapKeyDesc = (map: any) => {
    if (!map) return new Map();

    let list: any = [];
    for (let key1 of map.keys()) {
        list.push(key1);
    }
    let sortAsc1 = sortDesc(list);


    let newMap = new Map();
    for (let key2 of sortAsc1) {
        newMap.set(key2, map.get(key2));
    }

    return newMap;
};

export const sortAsc = (list: any) => {
    if (list) {
        return list.sort((n1: any, n2: any) => {
            if (n1 > n2) return 1;
            if (n1 < n2) return -1;
            return 0;
        });
    }
    return [];
};

export const sortDesc = (list: any) => {
    if (list) {
        return list.sort((n1: any, n2: any) => {
            if (n1 > n2) return -1;
            if (n1 < n2) return 1;
            return 0;
        });
    }
    return [];
};


